package cn.iocoder.yudao.module.eval.controller.admin.evaluator;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.evaluator.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.evaluator.EvalEvaluatorQualificationDO;
import cn.iocoder.yudao.module.eval.service.evaluator.EvalEvaluatorQualificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 考评员资质")
@RestController
@RequestMapping("/eval/evaluator-qualification")
@Validated
@Slf4j
public class EvalEvaluatorQualificationController {

    @Resource
    private EvalEvaluatorQualificationService evaluatorQualificationService;

    @PostMapping("/create")
    @Operation(summary = "创建考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:create')")
    public CommonResult<Long> createEvaluatorQualification(@Valid @RequestBody EvalEvaluatorQualificationSaveReqVO createReqVO) {
        return success(evaluatorQualificationService.createEvaluatorQualification(createReqVO));
    }

    @PostMapping("/batch-create-from-users")
    @Operation(summary = "从用户批量创建考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:create')")
    public CommonResult<Integer> batchCreateEvaluatorQualificationFromUsers(@Valid @RequestBody EvalEvaluatorQualificationBatchCreateFromUsersReqVO batchCreateReqVO) {
        return success(evaluatorQualificationService.batchCreateEvaluatorQualificationFromUsers(batchCreateReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:update')")
    public CommonResult<Boolean> updateEvaluatorQualification(@Valid @RequestBody EvalEvaluatorQualificationSaveReqVO updateReqVO) {
        evaluatorQualificationService.updateEvaluatorQualification(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考评员资质")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:evaluator:delete')")
    public CommonResult<Boolean> deleteEvaluatorQualification(@RequestParam("id") Long id) {
        evaluatorQualificationService.deleteEvaluatorQualification(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考评员资质")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:query')")
    public CommonResult<EvalEvaluatorQualificationRespVO> getEvaluatorQualification(@RequestParam("id") Long id) {
        EvalEvaluatorQualificationRespVO evaluatorQualification = evaluatorQualificationService.getEvaluatorQualificationRespVO(id);
        return success(evaluatorQualification);
    }

    @GetMapping("/page")
    @Operation(summary = "获得考评员资质分页")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:query')")
    public CommonResult<PageResult<EvalEvaluatorQualificationRespVO>> getEvaluatorQualificationPage(@Valid EvalEvaluatorQualificationPageReqVO pageReqVO) {
        PageResult<EvalEvaluatorQualificationDO> pageResult = evaluatorQualificationService.getEvaluatorQualificationPage(pageReqVO);
        return success(evaluatorQualificationService.buildEvaluatorQualificationRespVOPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考评员资质 Excel")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEvaluatorQualificationExcel(@Valid EvalEvaluatorQualificationPageReqVO exportReqVO,
                                                   HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalEvaluatorQualificationDO> list = evaluatorQualificationService.getEvaluatorQualificationList(exportReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "考评员资质.xls", "数据", EvalEvaluatorQualificationRespVO.class,
                BeanUtils.toBean(list, EvalEvaluatorQualificationRespVO.class));
    }

    @GetMapping("/get-by-user-id")
    @Operation(summary = "根据用户ID获得考评员资质")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:query')")
    public CommonResult<EvalEvaluatorQualificationRespVO> getEvaluatorQualificationByUserId(@RequestParam("userId") Long userId) {
        EvalEvaluatorQualificationDO evaluatorQualification = evaluatorQualificationService.getEvaluatorQualificationByUserId(userId);
        return success(BeanUtils.toBean(evaluatorQualification, EvalEvaluatorQualificationRespVO.class));
    }

    @PutMapping("/renew")
    @Operation(summary = "续期考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:update')")
    public CommonResult<Boolean> renewEvaluatorQualification(@RequestParam("id") Long id,
                                                              @RequestParam("validUntil") String validUntil) {
        evaluatorQualificationService.renewEvaluatorQualification(id, validUntil);
        return success(true);
    }

    @PutMapping("/suspend")
    @Operation(summary = "暂停考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:update')")
    public CommonResult<Boolean> suspendEvaluatorQualification(@RequestParam("id") Long id) {
        evaluatorQualificationService.suspendEvaluatorQualification(id);
        return success(true);
    }

    @PutMapping("/resume")
    @Operation(summary = "恢复考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:update')")
    public CommonResult<Boolean> resumeEvaluatorQualification(@RequestParam("id") Long id) {
        evaluatorQualificationService.resumeEvaluatorQualification(id);
        return success(true);
    }

    @PutMapping("/enable")
    @Operation(summary = "启用考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:update')")
    public CommonResult<Boolean> enableEvaluatorQualification(@RequestParam("id") Long id) {
        evaluatorQualificationService.enableEvaluatorQualification(id);
        return success(true);
    }

    @PutMapping("/disable")
    @Operation(summary = "禁用考评员资质")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:update')")
    public CommonResult<Boolean> disableEvaluatorQualification(@RequestParam("id") Long id) {
        evaluatorQualificationService.disableEvaluatorQualification(id);
        return success(true);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获得考评员资质统计")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:query')")
    public CommonResult<EvalEvaluatorQualificationStatisticsRespVO> getEvaluatorQualificationStatistics() {
        return success(evaluatorQualificationService.getEvaluatorQualificationStatistics());
    }

    @GetMapping("/list-by-status")
    @Operation(summary = "根据资质状态获得考评员列表")
    @Parameter(name = "qualificationStatus", description = "资质状态", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:evaluator:query')")
    public CommonResult<List<EvalEvaluatorQualificationRespVO>> getEvaluatorListByStatus(@RequestParam("qualificationStatus") Integer qualificationStatus) {
        List<EvalEvaluatorQualificationRespVO> list = evaluatorQualificationService.getEvaluatorListByStatus(qualificationStatus);
        return success(list);
    }

}